﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using HandLab.Mvc3App.Aspx.Models;
using HandLab.Mvc3App.Aspx.Models.Interfaces;
using HandLab.Mvc3App.Aspx.Models.Entities;
using HandLab.Mvc3App.Aspx.Controllers.Filters;

namespace HandLab.Mvc3App.Aspx.Controllers
{
    public class AnnounceController : Controller
    {

        public ActionResult Release()
        {
            ICategoryService ser = ServiceBuilder.BuildCategoryService();
            List<CategoryInfo> categories = ser.GetAll();
            ViewData["Categories"] = new SelectList(categories, "ID", "Name");
            return View("~/Aspx/Views/Announce/Release.aspx");
        }

        [LoggingFilter]
        [ExceptionFilter]
        public ActionResult DoRelease()
        {
            if (string.IsNullOrEmpty(Request.Form["Title"]) || string.IsNullOrEmpty(Request.Form["Content"]))
            {
                if (string.IsNullOrEmpty(Request.Form["Title"]))
                {
                    ViewData.ModelState.AddModelError("TitleValidator", "公告标题不能为空！");
                }

                if (string.IsNullOrEmpty(Request.Form["Content"]))
                {
                    ViewData.ModelState.AddModelError("ContentValidator", "公告内容不能为空！");
                }
                return Release();
            }

            AnnounceInfo announce = new AnnounceInfo()
            {
                ID = 1,
                Title = Request.Form["Title"],
                Category = Int32.Parse(Request.Form["Category"]),
                Content = Request.Form["Content"],
            };
            IAnnounceService ser = ServiceBuilder.BuilderAnnounceService();
            ser.Release(announce);
            ViewData["Announce"] = announce;

            System.Threading.Thread.Sleep(2000);
            ViewData["Time"] = DateTime.Now;
            System.Threading.Thread.Sleep(2000);
         
            return View("~/Aspx/Views/Announce/ReleaseSucceed.aspx");
        }
    }
}
